package com.ruoyi.system.service.impl;

import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.domain.SysContracts;
import com.ruoyi.system.domain.SysUserScok;
import com.ruoyi.system.mapper.SysUserScokMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.SysContractsService;
import com.ruoyi.system.mapper.SysContractsMapper;
import com.ruoyi.system.utils.MyMqttClient;
import com.ruoyi.system.utils.TCPClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * @author 86198
 * @description 针对表【sys_contracts】的数据库操作Service实现
 * @createDate 2024-06-24 19:13:15
 */
@Service
public class SysContractsServiceImpl
        implements SysContractsService {
    @Autowired
    private SysUserScokMapper userScokMapper;
    @Autowired
    private SysContractsMapper contractsMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    ISysUserService Userservice;

    @Override
    public boolean add(Integer num, String scokname, BigDecimal price, String scokId, Integer status) throws Exception {
        LoginUser loginUser = getLoginUser();
        Long userid = loginUser.getUserid();
        String username = loginUser.getUsername();
        System.out.println("------------" + userid);
//        SysContracts selectbyid = contractsMapper.getbyId(userid, scokId);
//        boolean b = StringUtils.isNotNull(selectbyid);
//        if (b){
//            BigDecimal chengjiao = new BigDecimal(num).multiply(price);
//            selectbyid.setScokPrice(selectbyid.getScokPrice().add(chengjiao));
//            selectbyid.setNum(selectbyid.getNum()+num);
//            selectbyid.setIndate(DateUtils.getNowDate());
//            //加入委托
//            contractsMapper.updateByScok(selectbyid);
//            //使用协议存储委托
//            new TCPClient().tcpClient(selectbyid,"0");
//            new MyMqttClient().myMqttClient(selectbyid);
//            new UDPClient().UDPClient(selectbyid);
//        }
//        else {
//            BigDecimal chengjiao = new BigDecimal(num).multiply(price);
        SysContracts contracts = new SysContracts();
        contracts.setScokPrice(price);
        contracts.setIndate(new Date());
        contracts.setScokName(scokname);
        contracts.setUserId(userid.intValue());
        contracts.setUname(username);
        contracts.setStatus(0);
        contracts.setFalg(status);
        contracts.setNum(num);
        contracts.setStockId(scokId);
        contractsMapper.inster(contracts);
        new TCPClient().tcpClient(contracts);
//        new MyMqttClient().myMqttClient();
//        }
        return true;
    }

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysContracts selectSysContractsById(Long id) {
        return contractsMapper.selectSysContractsById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysContracts 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysContracts> selectSysContractsList(SysContracts sysContracts) {
        return contractsMapper.selectSysContractsList(sysContracts);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysContracts 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysContracts(SysContracts sysContracts) {
        return contractsMapper.insertSysContracts(sysContracts);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysContracts 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysContracts(SysContracts sysContracts) {
        return contractsMapper.updateSysContracts(sysContracts);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysContractsByIds(Long[] ids) {
        return contractsMapper.deleteSysContractsByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysContractsById(Long id) {
        return contractsMapper.deleteSysContractsById(id);
    }

    @Override
    public SysUserScok selectByuidandcid(Long uid, String code) {
        return userScokMapper.getById(uid,code);
    }

    @Override
    public boolean updateStatus(Integer userId, String stockId, Integer falg, Integer num) {
        return contractsMapper.updateStatus(userId,stockId,falg,num);

    }

    @Override
    public void addContracts(SysContracts sysContracts) {

        contractsMapper.addContracts(sysContracts);
    }

    @Override
    public SysContracts getCodeNum(String stockId) {
        return contractsMapper.getCodeNum(stockId);
    }

    @Override
    public void updateSysContractsData(SysContracts sysContracts) {

    }

    @Override
    public void entrustmentStatus(Integer id) {
        contractsMapper.entrustmentStatus(id);
    }

    @Override
    public List<SysContracts> myEntrust(int parseInt) {
        return contractsMapper.myEntrust(parseInt);
    }

    @Override
    public void deleteContracts(SysContracts sysContracts) {
        if (sysContracts.getFalg()==0){
            //金额回填
            LoginUser loginUser = getLoginUser();
            Long userId = loginUser.getSysUser().getUserId();
            BigDecimal money = Userservice.selectUserById(userId).getMoney();
            BigDecimal multiply = sysContracts.getScokPrice().multiply(new BigDecimal(sysContracts.getNum()));
            money = money.add(multiply);
            Userservice.updateMoney(money, Math.toIntExact(userId));
            contractsMapper.deleteContracts(sysContracts.getId());
        }else {
            //股票回填
            SysUserScok sysUserScok = userScokMapper.getUserScok(sysContracts.getUserId(),sysContracts.getStockId());
            sysUserScok.setNum(sysUserScok.getNum()+sysContracts.getNum());
            sysUserScok.setIndate(new Date());
            int a = userScokMapper.updateScok(sysUserScok);

            if (a==0){
                throw new RuntimeException("回填失败");
            }else {
                contractsMapper.deleteContracts(sysContracts.getId());
            }
        }

    }


}




